package com.google.code.swingonfire.showcase.utils;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.EventListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeListener;

public class BooleanParameterDefinition extends AbstractParameterDefinition<JPanel, Boolean>
{

    private final String description;
    private final boolean defaultValue;

    public BooleanParameterDefinition(String name, String description, boolean defaultValue)
    {
        super(name, Boolean.class);

        this.description = description;
        this.defaultValue = defaultValue;
    }

    @Override
    public JPanel createComponent(EventListener eventListener)
    {
        JCheckBox checkBox = new JCheckBox();

        checkBox.setOpaque(false);
        checkBox.setSelected(defaultValue);
        checkBox.addActionListener((ActionListener) eventListener);
        checkBox.addChangeListener((ChangeListener) eventListener);

        JLabel label = new JLabel(description);

        label.setLabelFor(checkBox);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

        panel.setOpaque(false);
        panel.add(checkBox);
        panel.add(label);

        return panel;
    }

    @Override
    public Boolean getValue(JPanel component)
    {
        return ((JCheckBox) component.getComponent(0)).isSelected();
    }

    @Override
    public void setValue(JPanel component, Boolean value)
    {
        ((JCheckBox) component.getComponent(0)).setSelected(value);
    }

}
